//
//  Extension+UIButton.swift
//  WeiBlog
//
//  Created by xiang on 15/10/15.
//  Copyright © 2015年 xiang. All rights reserved.
//

import UIKit

extension UIButton {
   
    //分类里面的构造函数只能是 便利构造函数
    
    ///  设置 title,文字颜色,背景图片,点击事件,小图片(可选)
    convenience init(title:String?,fontSize:CGFloat?,color:UIColor?,bgImageName:String?,imageName:String?,actionName:String?,target:AnyObject?){
       
        self.init()
         
        setTitle(title, forState: UIControlState.Normal)
        setTitleColor(color, forState: UIControlState.Normal)
        
        //添加点击事件
        if let actionName = actionName{
            addTarget(target, action: Selector(actionName), forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        if let fontSize = fontSize{
            titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        }
        
        if let image = imageName {
            setImage(UIImage(named: image), forState: UIControlState.Normal)
            setImage(UIImage(named: image + "_highlighted"), forState: UIControlState.Highlighted)
        }
        
        if let bgImageName = bgImageName{
            setBackgroundImage(UIImage(named: bgImageName), forState: UIControlState.Normal)
            setBackgroundImage(UIImage(named: bgImageName + "_highlighted"), forState: UIControlState.Highlighted)
        }
        sizeToFit()
    }
    

}

